define(['jquery', 'domReady!', 'appLocalStorage', 'appConst', 'model/global', 'sockjs', 'appMessageBox'], function ($, domReady, appLocalStorage, appConst, global, SockJS, msgBox) {
    var userId = global.getUserId();
    return {
        initExport: function () {
            // var that = this;
            // var socket = new SockJS("/wst");
            // var stompClient_co = Stomp.over(socket);

            var socket1 = new SockJS("/wst");
            var stompClient_co1 = Stomp.over(socket1);
            // stompClient_co.debug = false;
            stompClient_co1.debug = false;
            stompClient_co1.connect("guest", "guest", function () {
                stompClient_co1.subscribe('/topic/data/exportDownloadListener#' + userId, function (frame) {
                    var data = eval('(' + frame.body + ')');
                    if (data.result == "success") {
                        $('#tb_exportHistory').dataTable().api().ajax.reload();
                        var form = $("<iframe>");
                        form.attr("style", "display:none");
                        form.attr("src", data.url);
                        $("body").append(form);
                        $('#grr_export_message').parents('div.SmallBox').remove();
                    }
                    else  {
                        $('#grr_export_message').parents('div.SmallBox').remove();
                        msgBox.show({
                            content: "Export Failure !",
                            type:'error'
                        });
                    }
                });
            }, function (error) {
                console.log(error);
            });
        },
        initImport: function (soltkey, callback) {
            var userId = global.getUserId();
            var sockjs = new SockJS("/wst");
            var stomp = Stomp.over(sockjs);
            stomp.debug = false;

            stomp.connect("guest", "guest", function () {
                stomp.subscribe('/topic/data/importListener#' + soltkey + '#' + userId, function (frame) {
                    var pdata = eval('(' + frame.body + ')');
                    if($.isFunction(callback)){
                        callback(pdata);
                    }
                });

            }, function (error) {
                console.log(error);
            });
            return {sockjs:sockjs, stomp:stomp};
        },
        receivedProcess: function (d, opt) {
            var processbar = opt.processbar;
            var pstatus = opt.pstatus;
            var poperator = opt.poperator;
            var plog = opt.plog;
            var psubmit = opt.psubmit;
            var timeoutKey = opt.ptimeoutkey;
            var isLastUpdate = d.lastUpdate;

            pstatus.empty();
            processbar.addClass('progress-bar');
            processbar.attr("aria-valuenow", Math.round(d.rate));
            processbar.css('width', Math.round(d.rate) + '' + '%');
            poperator.html($.i18n.prop('DATAIMPORT_ON_OPERATING', Math.round(d.rate)));
            var readSize = d.readSize / (1000 * 1000);
            var totalSize = d.totalSize / (1000 * 1000);
            pstatus.append(Math.round(readSize * 100) / 100 + "/" + Math.round(totalSize * 100) / 100 + " M");
            var log = d.log.replace(/\\\\n/g, "<br>");

            if (plog != null && plog != "" && plog != undefined) {
                plog.append(log);
                plog.scrollTop(plog[0].scrollHeight);
            }

            var timeout;
            var queryParameter = appLocalStorage.getLocal(timeoutKey);
            if (queryParameter != null) {
                queryParameter = JSON.parse(queryParameter);
                timeout = queryParameter.timeout;
            }

            if (log != null && log != "" && (log.indexOf("empty") >= 0 || log.indexOf("without") >= 0 || log.indexOf("Failed") >= 0)) {
                if(isLastUpdate == true) {
                    processbar.addClass('progress-bar-danger');
                    psubmit.button("reset");
                    clearTimeout(timeout);
                }
            }

            if (d.rate == 100) {
                processbar.addClass('progress-bar-success');
                poperator.html($.i18n.prop('DATAIMPORT_ON_complete', 100));
                pstatus.html(Math.round(totalSize * 100) / 100 + "/" + Math.round(totalSize * 100) / 100 + " M");
                psubmit.button("reset");
                clearTimeout(timeout);
            }

        }
    }
});